package com.cj.webapp.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * HFJsonUtil.java
 * @author lb
 * 2017年2月21日
*/
public class HFJsonUtil {
	
	public static String beanToJson(Object object){
		String strJson ="";
		try {
			strJson =JSON.toJSONString(object);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return strJson;
	}

	public static <T> T getBean(String jsonString, Class<T> cls) {
		T t = null;
		try {
			t = JSON.parseObject(jsonString, cls);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return t;
	}
	
	public static Map<String, String> getJsonToMap(String jsonStr){
		Map<String, String> map=new HashMap<String, String>();
		JSONObject jsonObject=JSON.parseObject(jsonStr);
		Set<Entry<String, Object>> setMap=jsonObject.entrySet();
		for (Entry<String, Object> entry :setMap)  {
			if(entry.getValue() != null){
				map.put(entry.getKey().toString(), entry.getValue().toString());
			}

		}

		return map;
		
	}
}
